package com.graduate.game.user.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.graduate.game.user.pojo.userdata;
import com.graduate.game.user.service.UserService;
import com.graduate.game.user.service.UserdataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/userdata")
@CrossOrigin(origins = "*", maxAge = 3600)
public class UserdataController {
    @Autowired
    public UserdataService userdataService;
    //获取某个用户的金币和经验值
    @RequestMapping("/onedata")
    public userdata getoneuserdata(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        return userdataService.getOneUserdata(s1);
    }
    //金币排行榜总数
    @RequestMapping("/goldrank")
    public int goldRank(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        return userdataService.goldRank().size();
    }

    //金币排行榜总数
    @RequestMapping("/experiencerank")
    public int experienceRank(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        return userdataService.experienceRank().size();
    }
    //金币排行榜的分页查询
    @RequestMapping("/querystudentbypage")
    public List<userdata> querystudentbypage(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map) JSON.parse(s1);
        int pageNum=Integer.parseInt(maps.get("currentPage").toString());
        int pagesize=Integer.parseInt(maps.get("pagesize").toString());
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",(pageNum-1)*pagesize);
        data.put("pageSize",pagesize);
        List<userdata> allUserByPageS = userdataService.findAllUserByPageS(data);
        return allUserByPageS;
    }
    //校力值排行榜的分页查询
    @RequestMapping("/experienceRankByPageS")
    public List<userdata> experienceRankByPageS(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map) JSON.parse(s1);
        int pageNum=Integer.parseInt(maps.get("currentPage").toString());
        int pagesize=Integer.parseInt(maps.get("pagesize").toString());
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",(pageNum-1)*pagesize);
        data.put("pageSize",pagesize);
        List<userdata> allUserByPageS = userdataService.experienceRankByPageS(data);
        return allUserByPageS;
    }
    //更新金币
    @RequestMapping("/updategold")
    public String updategold(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map)JSON.parse(s1);
        userdata userdata = new userdata();
        userdata getuserdata = userdataService.getOneUserdata((String) maps.get("username"));
        long gold = getuserdata.getGold();
        gold=gold-Integer.parseInt(maps.get("gold").toString());
        userdata.setGold(gold);
        userdata.setUsername((String) maps.get("username"));
        userdataService.updateGoldRank(userdata);
        return "金币更新成功";
    }

}
